/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
package multimedia.control.business
{
	import com.adobe.cairngorm.business.ServiceLocator;
	
	import multimedia.model.ModelLocator;
	import multimedia.model.vo.FeedItemVO;
	import multimedia.model.vo.FeedVO;
	import multimedia.model.vo.UserVO;
	
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.http.HTTPService;
	import mx.rpc.remoting.RemoteObject;
	
	/**
	 * delegate making service calls for feed stuff
	 */
	public class FeedDelegate
	{
		/**
		 * the responder which should be called on result or fault
		 */
		private var responder:IResponder;
		/**
		 * the service to call
		 */
		private var service:RemoteObject = ServiceLocator.getInstance().getRemoteObject("feeds");
		private var httpService:HTTPService = ServiceLocator.getInstance().getHTTPService("feedUpdate");
		
		/**
		 * constructor
		 *  
		 * @param responder the responder which should be called on result or fault
		 */
		public function FeedDelegate(responder:IResponder)
		{
			this.responder = responder;
		}
		
		/**
		 * service call to get all feeds
		 */
		public function getFeeds():void
		{
			var token:AsyncToken = service.getFeeds();
			token.addResponder(responder);
		}
		
		/**
		 * service call to save a feed
		 * 
		 * @param feed the <code>FeedVO</code> containing all information
		 * @param user the user who added the feed
		 */ 
		public function saveFeed(feed:FeedVO, user:UserVO):void
		{
			var token:AsyncToken = service.saveFeed(feed, user);
			token.addResponder(responder);
		}
		
		/**
		 * update a feed, for example valid date or trust level
		 * 
		 * @param feed the feed to update
		 */
		public function updateFeed(feed:FeedVO):void
		{
			var token:AsyncToken = service.updateFeed(feed);
			token.addResponder(responder);
		}
		
		/**
		 * delete a feed from database
		 * 
		 * @param feed the feed to delete
		 */
		public function deleteFeed(feed:FeedVO):void
		{
			var token:AsyncToken = service.deleteFeed(feed);
			token.addResponder(responder);
		}
		
		/**
		 * get all feed items belonging to a certain feed
		 * 
		 * @param feed the feed to get the items to
		 */
		public function getFeedItems(feed:FeedVO):void
		{
			var token:AsyncToken = service.getFeedItems(feed);
			token.addResponder(responder);
		}
		
		/**
		 * 
		 * @param feed
		 */
		public function updateFeedItems(feed:FeedVO):void
		{
			httpService.url = ModelLocator.getInstance().webserviceURL + "/updateFeeds";
			var params:Object = new Object();
			params.apiKey = ModelLocator.getInstance().apiKey;
			params.feed_id = feed.feed_id;
			var token:AsyncToken = httpService.send(params);
			token.addResponder(responder);
		}
		
		/**
		 * update the status of a feed item ('include'|'exclude')
		 * 
		 * @param feedItem the item to update
		 */ 
		public function updateFeedItemStatus(feedItem:FeedItemVO):void
		{
			var token:AsyncToken = service.updateFeedItemStatus(feedItem);
			token.addResponder(responder);
		}

	}
}